package com.pig4cloud.mall.product.util;

import com.pig4cloud.mall.product.dto.CategoryTree;
import com.pig4cloud.mall.product.dto.CidTree;
import com.pig4cloud.mall.product.dto.TreeNode;
import com.pig4cloud.mall.product.entity.Category;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fangyu
 * @version v1.0.0
 * @date 2020/11/25 3:50 下午
 */
@Slf4j
@UtilityClass
public class TreeUtil {

	/**
	 * 两层循环实现建树
	 *
	 * @param treeNodes 传入的树节点列表
	 * @return
	 */
	public <T extends TreeNode> List<T> build(List<T> treeNodes, Object root) {
		List<T> trees = new ArrayList<>();

		for (T treeNode : treeNodes) {

			if (root.equals(treeNode.getParentCid())) {
				trees.add(treeNode);
			}

			for (T it : treeNodes) {
				if (it.getParentCid() == treeNode.getCatId()) {
					if (treeNode.getChildren() == null) {
						treeNode.setChildren(new ArrayList<>());
					}
					treeNode.add(it);
					treeNode.setHasChildren(true);
				}
			}
		}
		return trees;
	}

	/**
	 * 通过Category创建三级树形节点
	 *
	 * @param categories
	 * @param root
	 * @return
	 */
	public List<CategoryTree> buildTree(List<Category> categories, long root) {
		List<CategoryTree> trees = new ArrayList<>();
		CategoryTree node;
		for (Category category : categories) {
			node = new CategoryTree();
			node.setCatId(category.getCatId());
			node.setParentCid(Math.toIntExact(category.getParentCid()));
			node.setName(category.getName());
			node.setShowStatus(category.getShowStatus());
			node.setIcon(category.getIcon());
			node.setProductUnit(category.getProductUnit());
			node.setProductCount(category.getProductCount());
			node.setSort(category.getSort());
			// FIXME children 全部为false or true
			node.setHasChildren(true);
			trees.add(node);
		}
		return TreeUtil.build(trees, root);
	}

	/**
	 * 创建两级树形
	 *
	 * @param treeNodes 传入的树节点列表
	 * @return
	 */
	public List<CidTree> buildCid(List<CidTree> treeNodes, Object root, Integer depth) {
		List<CidTree> trees = new ArrayList<>();

		for (CidTree treeNode : treeNodes) {

			if (root.equals(treeNode.getPid())) {
				trees.add(treeNode);
			}

			for (CidTree it : treeNodes) {
				// 如果大于需要获取的最大深度，则break
				if (it.getLevel() > depth) {
					break;
				}
				if (it.getPid().equals(treeNode.getValue())) {
					if (treeNode.getChildren() == null) {
						treeNode.setChildren(new ArrayList<>());
					}
					treeNode.add(it);
				}
			}
		}
		return trees;
	}


	/**
	 * TODO
	 * 通过Category构建二级树形 cid节点 (三级需要被过滤)
	 * @param selectList
	 * @param categoryTreeRootId
	 * @return
	 */
	public List<CidTree> buildTreeCid(List<Category> selectList, Long categoryTreeRootId, Integer depth) {
		List<CidTree> trees = new ArrayList<>();
		CidTree cidTree;
		for (Category category : selectList) {
			cidTree = new CidTree();
			cidTree.setValue(category.getCatId());
			cidTree.setLabel(category.getName());
			cidTree.setPid(category.getParentCid());
			cidTree.setLevel(category.getCatLevel());
			trees.add(cidTree);
		}
		return TreeUtil.buildCid(trees, categoryTreeRootId, depth);
	}


}
